<?php
namespace Admin\Controller;
use Admin\Model\AuthGroupModel;
use Think\Page;

/**
 * Description of DemoController
 *
 * @author YourName
 */
class SpecController extends AdminController {

    /**
    * 列表
    * @author YourName <YourEmail>
    */
    public function index(){
		//$this->getMenu();
        $name = I('name');
        $map['name'] = array('like', '%' . (string) $name . '%');
        $list = $this->lists('spec', $map,'disorder desc,addtime desc');
        int_to_string($list);
        $this->assign('_list', $list);
      
        $this->display();
    }

    /**
    * 移除文章
    * @author YourName <YourEmail>
    */
    public function yichu(){
        $id = intval($_POST['id']); 
		$zid = intval($_POST['zid']); 
		if($id>0)
		{
	
		  M('speclist')->where(array('aid' =>$id,'sid'=>$zid))->delete();
		}
    }
    /**
    * 添加
    * @authdor YourName <YourEmail>
    */
    public function add($cid="",$title='',$thumb='',$demo='',$disorder='',$tag='',$postion=''){
		//echo '111';
         $pos = C("DOCUMENT_POSITION");
		 $this->assign('pos',$pos);
         $cat = M('category')->where(array('pid'=>0))->select(); 
		 $this->assign('cat',$cat);
         if(IS_POST){
		$pos = $_POST['postion'];
            $spec = array(
				'title'=>$title,
				'thumb'=>$thumb,
				'demo'=>$demo,
				'addtime'=>time(),
				'tag'=>$tag,
					'cid'=>$cid,
				'postion'=>$pos[0]
			
            ); 
            $id = M('spec')->add($spec);
            if (!$id) {
            	$this->error('添加失败！');
            } else {
            	$this->success('添加成功！', U('index'));
            };
         }else{
            $this->display();
         }
     }

     /**
     * 编辑
     * @author YourName <YourEmail>
     */
    public function edit($id="",$cid="",$title='',$thumb='',$demo='',$tag='',$position=''){ 
		 $pos = C("DOCUMENT_POSITION");
		 $this->assign('pos',$pos);
         $cat = M('category')->where(array('pid'=>0))->select(); 
		 $this->assign('cat',$cat);
        if(IS_POST){ 
			
		///	$pos = $_POST['postion'];print_R($pos);
            $spec = array(			
				'title'=>$title,
				'thumb'=>$thumb,
				'demo'=>$demo,
			     'tag'=>$tag,
				'cid'=>$cid,
				'position'=>$position[0],	
            );
             if (!M('spec')->where(array('id' => $id))->save($spec)) {
                $this->error('修改失败！');
            } else {
                $this->success('修改成功！', U('index'));
            };
        }else{
            $data = M('spec')->where(array('id' => $id))->find();
            $this->data = $data;
            $this->display();
        }
    }


    /**
    * 删除方法
    * @author YourName <YourEmail>
    */
    public function del($id = ''){
         if(!is_null($id)){
             if (!M('spec')->where(array('id' => $id))->delete()){
                $this->error('删除失败！');
            } else {
                $this->success('删除成功！', U('index'));
            };
        }
    }
    public function Upsort($id = '',$sort=0){
		 
         if(!is_null($id) && $sort>0){
             M('spec')->where(array('id' => $id))->save(array('disorder'=>intval($sort)));                
        }
    }

    /**
    * 状态修改
    * @author YourName <YourEmail>
    */
    public function changeStatus($method = null, $id) {
       $id = is_array($id) ? implode(',', $id) : $id;
       if (empty($id)) {
           $this->error('请选择要操作的数据!');
       }
       $map['id'] = array('in', $id);
	   
       switch (strtolower($method)) {
           case 'forbid':
               $this->forbid('spec',$map);
               break;
           case 'resume':
			
               $this->resume('spec', $map);
               break;
           case 'delete':
               $this->del($id);
               break;
           default:
               $this->error('spec');
       }
    }

    /**
    * 添加文章
    * @author YourName <YourEmail>
    */
    public function addxin() {
	   $id = $_POST['zid'];
	   $ids = $_POST['allid'];
      
       if (empty($id)) {
           $this->error('你还没选择专题!');
       }
	          if (empty($ids)) {
           $this->error('没有添加文章');
       }
	   $ids = @explode(',', $ids);
	 //  $ids = @implode(',', $ids);
	 if(!empty($ids)){
       $map['id'] = array('in', $ids);
	   		   foreach($ids as $v)
			{
              if($v)
				{
				   $coun = M('speclist')->where(array('aid'=>$v,'sid'=>$id))->count();// echo $coun;exit;
				   if($coun>0)
				   {
                     $this->error('文章编号：'.$v.' 已存在');
				   }
				}
			}
	 }else{
		 $map['id'] = 0;
	 }
 

		
	
	   $rs = M('document')->field('id,create_time,description,title,category_id,cover_id')->where($map)->select();
	   if(!empty($rs))
		{
		   foreach($rs as $key=>$val)
			{
			   if($val['title'])
				{ $img = get_cover($val['cover_id']);
				   $data['sid'] = $id; //专题id
				   $data['title'] = $val['title'];
				   $data['aid'] = $val['id'];
				   $data['demo'] = $val['description'];
				   $data['imgurl'] = $img['path']; 
				   // $data['addtime'] = $val['create_time'];
                   $data['addtime'] = time();   // @author linrui
				   M('speclist')->add($data);
				   unset($data);
				}
			}
			$this->success('修改成功！', U('index'));
		}else
		{
			 $this->error('文章不存在');
		}

    }

	    /**
     * 分类文档列表页
     * @param integer $cate_id 分类id
     * @param integer $model_id 模型id
     * @param integer $position 推荐标志
     * @param integer $group_id 分组id
     */
    public function mydocument($zid = null,$status = null, $title = null){

        if(empty($zid)){
           $this->error('选择专题');
        }
        $Document   =   D('Document');
        /* 查询条件初始化 */
        $rs = M("speclist")->where(array('sid'=>$zid))->select();
		if(!empty($rs))
		{
				foreach($rs as $key=>$val)
				{
					if($val['aid']){
					  $ids[] = $val['aid'];
					}
				}
					$map['id']  = array('in', $ids);
			
			
		
			if(isset($title)){
				$map['title']   =   array('like', '%'.$title.'%');
			}
			if(isset($status)){
				$map['status']  =   $status;
			}else{
				$map['status']  =   array('in', '0,1,2');
			}
			if ( isset($_GET['time-start']) ) {
				$map['update_time'][] = array('egt',strtotime(I('time-start')));
			}
			if ( isset($_GET['time-end']) ) {
				$map['update_time'][] = array('elt',24*60*60 + strtotime(I('time-end')));
			} 
			//只查询pid为0的文章
		   $list = $this->lists($Document,$map,'update_time desc');

			$list = $this->parseDocumentList($list,1);
        }else
		{
			$list=array();
			$status=array();
		}
        // 记录当前列表页的cookie
        Cookie('__forward__',$_SERVER['REQUEST_URI']);
        $this->assign('status', $status);
		$this->assign('zid', $zid);
        $this->assign('list', $list);
        $this->meta_title = '专题管理信息';
        $this->display();
    }


}